package models;
 
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.persistence.*;

import com.jamonapi.utils.Logger;

import controllers.CrudMember;
import net.sf.oval.constraint.DateRange;

 
import play.data.validation.Email;
import play.data.validation.Match;
import play.data.validation.Required;
import play.db.jpa.*;

/**
 * Member of a club
 */
@Entity
public class Member extends Model {
 
	@Required
    public String name;
	@Email
    public String email;
    public String hometown;
    public String address;
    @Transient
    public String active;
    @Transient
    public Boolean selected;
    @Transient
    public String stringActivationEnds;
   

	/**
     * The club the member belongs to
     */
    @ManyToOne
    public ClubUser club;
    
    /**
     * Time when the activation ends. If this is null, it means that the
     * activation never ends
     */
    
    public Date activationEnds;
    
    public Member(ClubUser club, String name, String email, String hometown, 
    		String address, Date activationEnds) {
    	this.club = club;
        this.name = name;
        this.email = email;
        this.hometown = hometown;
        this.address = address;
        this.activationEnds = activationEnds;
    }
    
    /**
     * Returns user activation status. 
     * 
     * User is active if current time is before activationEnds 
     * time or if activationEnds is null
     * 
     * @return true if user is active
     */
    public Boolean isActive() {
    	if(activationEnds == null) {
    		return true;
    	} else {
    		if(activationEnds.after(new Date())) {
    			return true;
    		} else {
    			return false;
    		}
    	}
	}
    
    /**
     * Returns user activation status. 
     * 
     * User is active if current time is before activationEnds 
     * time or if activationEnds is null
     * 
     * @return true if user is active
     */
    public String getActive() {
    	if(activationEnds == null) {
    		this.active = "active";
    	} else {
    		if(activationEnds.after(new Date())) {
    			this.active = "active";
    		} else {
    			this.active = "-";
    		}
    	}
		return active;
	}

	public String toString() {
    	return this.name;
    }
	
	public static List<Member> findActiveMembersByNameAndClubs(String name, List<ClubUser> clubs) {
		if(clubs.size() == 0) {
			return new ArrayList<Member>();
		}
		Query query = JPA.em().createQuery("SELECT m FROM Member m WHERE upper(m.name) like :name AND (m.activationEnds > :now OR m.activationEnds IS NULL) AND m.club IN :clubs");
		query.setParameter("name", "%" + name.toUpperCase() + "%");
    	query.setParameter("now", new Date(), TemporalType.DATE);
    	query.setParameter("clubs", clubs);
    	List<Member> members = query.getResultList();
		
		return members;
	}

	public String getStringActivationEnds() {
		if (this.activationEnds != null) {
			DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			return dateFormat.format(this.activationEnds);
		}
		return "Never";
	}
	
	
}